package com.voole.service.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.voole.dao.SysRoleActionDao;
import com.voole.dao.SysRoleDao;
import com.voole.dao.SysUserDao;
import com.voole.entity.SysRole;
import com.voole.entity.SysRoleAction;
import com.voole.entity.SysUser;
import com.voole.service.ISysRoleService;
import com.voole.service.ISysUserService;

@Service
@Transactional(propagation = Propagation.REQUIRED)
public class SysRoleServiceImpl implements ISysRoleService {

	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private SysRoleActionDao sysRoleActionDao;

	/**
	 * 权限列表（分页）
	 */
	public Map<String, Object> findByPage(String start, String length) throws Exception {
		//计算分页行数
		Integer end = Integer.parseInt(start) + Integer.parseInt(length);
		//获取分页内容总数
		Integer count = sysRoleDao.getCount();
		//获取分页当前内容
		List<SysRole> list = sysRoleDao.findByPage(Integer.parseInt(start), end);
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", list);
		map.put("recordsTotal", list.size());//pageSize
		map.put("recordsFiltered", count);//count
		return map;
	}
	
	/**
	 * 权限列表（分页）
	 */
	public List<SysRole> getList() throws Exception {
		return sysRoleDao.getList();
	}

	/**
	 * 新建权限
	 */
	public Map<String, Object> addSysRole(String roleName, String roleEn, Integer status) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		
		SysRole sysRole = sysRoleDao.getSysRole(roleName, roleEn);
		if(sysRole != null){
			result.put("msg", "失败, 内容重复");
			return result;
		}
		SysRole sr = new SysRole();
		sr.setName(roleName);
		sr.setRole(roleEn);
		sr.setStatus(status);
		sysRoleDao.addSysRole(sr);
		
		result.put("msg", "操作成功");
		return result;
	}

	/**
	 * 设置权限
	 */
	public Map<String, Object> setSysRole(String roleId, String actionIds) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();

		if(StringUtils.isEmpty(roleId) || StringUtils.isEmpty(actionIds) ){
			result.put("msg", "失败, 参数不完整");
			return result;
		}
		String[] actionIdArray = actionIds.split(",");
		//删除原有
		sysRoleActionDao.delSysRoleAction(Integer.parseInt(roleId));
		//保存新的对应关系
		
		for(String actionId: actionIdArray){
			//过滤上级无用参数，例如j2_2
			if(actionId.indexOf("j")>-1){
				continue;
			}
			//保存
			SysRoleAction sra = new SysRoleAction();
			sra.setRoleid(Integer.parseInt(roleId));
			sra.setActionid(Integer.parseInt(actionId));
			sysRoleActionDao.addSysRoleAction(sra);
		}
		
		result.put("msg", "操作成功");
		return result;
	}
	
}
